package net.hn.hnms.biz.plan.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.hn.hnms.biz.plan.domain.DimMiningFace;
import net.hn.hnms.biz.plan.domain.ExecutionFootageOutput;
import net.hn.hnms.biz.plan.mapper.ExecutionFootageOutputMapper;
import net.hn.hnms.biz.plan.request.CooperativeMiningFacReq;
import net.hn.hnms.biz.plan.service.CooperativeMiningFacService;
import net.hn.hnms.biz.plan.vo.CooperativeMiningFacVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * 协同开采
 *
 * @author gzr
 * @date 2023-03-02
 */
@Service

public class CooperativeMiningFacServiceImpl implements CooperativeMiningFacService {
    @Autowired
    private DimMiningFaceServiceImpl dimMiningFaceServiceImpl;
    @Autowired
    private ExecutionFootageOutputMapper executionFootageOutputMapper;

    @Override
    public CooperativeMiningFacVo selectData(CooperativeMiningFacReq req) {
        DimMiningFace dimMiningFace = dimMiningFaceServiceImpl.getDimMiningFace(req);
        if (dimMiningFace == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        CooperativeMiningFacVo cooperativeMiningFacVo = new CooperativeMiningFacVo();
        //获取该回采工作面总长度 2558.00
        String id = dimMiningFace.getMiningFaceId();
        BigDecimal miningFaceLength = dimMiningFace.getMiningFaceLength();

        //根据工作面id查询昨日产量
        LocalDate localDate = LocalDate.now(); //获取今天的日期
        LocalDate yesterday = localDate.plusDays(-1); //前一天日期是今天减1
        LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());

        BigDecimal yestdayOutput = getOutput(id, yesterday);
        BigDecimal monthOutput = getOutput(id, yesterday, firstDay);
        BigDecimal nowfoo = getfootage(id, req.getDate());//当日进尺
        //已经采的米数 881.1
        BigDecimal currentlyMined = getfootage(id);
        //已经采的米数  根据日期
        BigDecimal currentlyMineds = getfootages(id,req.getDate());
        cooperativeMiningFacVo.setYesterdayOutput(yestdayOutput);
        cooperativeMiningFacVo.setNowFootage(nowfoo);
        cooperativeMiningFacVo.setMonthOutput(monthOutput);
        cooperativeMiningFacVo.setCurrentlyMined(currentlyMined);
        cooperativeMiningFacVo.setCurrentlyMineds(currentlyMineds);
        cooperativeMiningFacVo.setOverlength(miningFaceLength.subtract(currentlyMined));
        cooperativeMiningFacVo.setOverslength(miningFaceLength.subtract(currentlyMineds));
        if (miningFaceLength.compareTo(new BigDecimal(0)) > 0) {
            BigDecimal decimal = (currentlyMined.divide(miningFaceLength, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))).setScale(0,BigDecimal.ROUND_HALF_UP);
            cooperativeMiningFacVo.setPercentum(decimal);
        }
        //采煤天数，359
        int day = executionFootageOutputMapper.selectDay(id);
        if (day!=0){
            BigDecimal dayFootage = currentlyMined.divide(new BigDecimal(day),4,RoundingMode.HALF_UP);
            if (dayFootage.compareTo(new BigDecimal(0))!=0){
                BigDecimal days = (miningFaceLength.subtract(currentlyMined)).divide(dayFootage, 0, RoundingMode.HALF_UP);
                cooperativeMiningFacVo.setDays(days);
            }
        }
        if (day==0){
            cooperativeMiningFacVo.setDays(new BigDecimal(0).setScale(0,BigDecimal.ROUND_HALF_UP));
        }

        return cooperativeMiningFacVo;
    }


    /**
     * 获取产量
     *
     * @param id
     * @param yesterday
     * @return
     */
    private BigDecimal getOutput(String id, LocalDate yesterday) {
        BigDecimal output = new BigDecimal(0);
        LambdaQueryWrapper<ExecutionFootageOutput> footageWrapper = new LambdaQueryWrapper<>();
        footageWrapper.eq(ExecutionFootageOutput::getWorkFaceId, id).eq(ExecutionFootageOutput::getExecutionDate, yesterday);
        List<ExecutionFootageOutput> executionFootageOutputs = executionFootageOutputMapper.selectList(footageWrapper);
        if (executionFootageOutputs.size() > 0) {
            //executionFootageOutputs.forEach(outputvo -> output.add(outputvo.getOutput()));
            for (ExecutionFootageOutput executionFootageOutput : executionFootageOutputs) {
                BigDecimal output1 = executionFootageOutput.getOutput();
                if (output1 == null) {
                    output1 = new BigDecimal(0);
                }
                output = output.add(output1);
            }
        }
        return output;
    }

    /**
     * 获取产量
     *
     * @param id
     * @param yesterday
     * @param firstDay
     * @return
     */
    private BigDecimal getOutput(String id, LocalDate yesterday, LocalDate firstDay) {
        BigDecimal output = new BigDecimal(0);
        BigDecimal footage = new BigDecimal(0);
        LambdaQueryWrapper<ExecutionFootageOutput> footageWrapper = new LambdaQueryWrapper<>();
        footageWrapper.eq(ExecutionFootageOutput::getWorkFaceId, id)
                .ge(ExecutionFootageOutput::getExecutionDate, firstDay)
                .le(ExecutionFootageOutput::getExecutionDate, yesterday);
        List<ExecutionFootageOutput> executionFootageOutputs = executionFootageOutputMapper.selectList(footageWrapper);
        if (executionFootageOutputs.size() > 0) {
            //executionFootageOutputs.forEach(outputvo -> output.add(outputvo.getOutput()));
            for (ExecutionFootageOutput executionFootageOutput : executionFootageOutputs) {
                BigDecimal output1 = executionFootageOutput.getOutput();
                if (output1 == null) {
                    output1 = new BigDecimal(0);
                }
                output = output.add(output1);
            }
        }
        return output;
    }

    /**
     * 根据工作面id获取该工作面的进尺
     *
     * @param id
     * @return
     */
    private BigDecimal getfootage(String id) {
        BigDecimal footage = new BigDecimal(0);
        LambdaQueryWrapper<ExecutionFootageOutput> footageWrapper = new LambdaQueryWrapper<>();
        footageWrapper.eq(ExecutionFootageOutput::getWorkFaceId, id);
        List<ExecutionFootageOutput> executionFootageOutputs = executionFootageOutputMapper.selectList(footageWrapper);
        if (executionFootageOutputs.size() > 0) {
            //executionFootageOutputs.forEach(outputvo -> footage.add(outputvo.getOutput()));
            for (ExecutionFootageOutput executionFootageOutput : executionFootageOutputs) {
                BigDecimal footage1 = executionFootageOutput.getFootage();
                if (footage1 == null) {
                    footage1 = new BigDecimal(0);
                }
                footage = footage.add(footage1);
            }
        }
        return footage;
    }
    private BigDecimal getfootages(String id,LocalDate date) {
        BigDecimal footage = new BigDecimal(0);
        LambdaQueryWrapper<ExecutionFootageOutput> footageWrapper = new LambdaQueryWrapper<>();
        footageWrapper.eq(ExecutionFootageOutput::getWorkFaceId, id).le(ExecutionFootageOutput::getExecutionDate, date);
        List<ExecutionFootageOutput> executionFootageOutputs = executionFootageOutputMapper.selectList(footageWrapper);
        if (executionFootageOutputs.size() > 0) {
            //executionFootageOutputs.forEach(outputvo -> footage.add(outputvo.getOutput()));
            for (ExecutionFootageOutput executionFootageOutput : executionFootageOutputs) {
                BigDecimal footage1 = executionFootageOutput.getFootage();
                if (footage1 == null) {
                    footage1 = new BigDecimal(0);
                }
                footage = footage.add(footage1);
            }
        }
        return footage;
    }


    private BigDecimal getfootage(String id, LocalDate yesterday) {
        BigDecimal footage = new BigDecimal(0);
        LambdaQueryWrapper<ExecutionFootageOutput> footageWrapper = new LambdaQueryWrapper<>();
        footageWrapper.eq(ExecutionFootageOutput::getWorkFaceId, id)
             .eq(ExecutionFootageOutput::getExecutionDate, yesterday);
        List<ExecutionFootageOutput> executionFootageOutputs = executionFootageOutputMapper.selectList(footageWrapper);
        if (executionFootageOutputs.size() > 0) {
            //executionFootageOutputs.forEach(outputvo -> footage.add(outputvo.getOutput()));
            for (ExecutionFootageOutput executionFootageOutput : executionFootageOutputs) {
                BigDecimal footage1 = executionFootageOutput.getFootage();
                if (footage1 == null) {
                    footage1 = new BigDecimal(0);
                }
                footage = footage.add(footage1);
            }
        }
        return footage;
    }
}



